package plugins

import (
	"errors"
	"strconv"
)

type (
	MsgType    uint16
	MsgSubType uint16
)

func Extract(typ uint32) (MsgType, MsgSubType, error) {
	major := (typ & 0xff00) >> 16
	minor := typ & 0x00ff

	majorTyp := majorMsgTypes[uint16(major)]
	minorTyp := minorMsgTypes[uint16(major)]

	if majorTyp == 0 {
		return 0, 0, errors.New("cant find major msg type for value " + strconv.Itoa(int(major)))
	}

	if minorTyp == 0 {
		return 0, 0, errors.New("cant find minor msg type for value " + strconv.Itoa(int(minor)))
	}

	return majorTyp, minorTyp, nil
}

const (
	// 消息主类型
	SingleChatMsg MsgType = 0x01
	GroupChatMsg  MsgType = 0x02

	// 子类型
	TextMsg              MsgSubType = iota
	ImageMsg             MsgSubType = 0x02
	VideoMsg             MsgSubType = 0x03
	AudioMsg             MsgSubType = 0x04
	LocationMsg          MsgSubType = 0x05
	BusinessCardMsg      MsgSubType = 0x06
	GroupAnnouncementMsg MsgSubType = 0x07
	EmojiMsg             MsgSubType = 0x08
	FileMsg              MsgSubType = 0x09
	RedPacketMsg         MsgSubType = 0x10
)

var majorMsgTypes = map[uint16]MsgType{
	0x01: SingleChatMsg,
	0x02: GroupChatMsg,
}

var minorMsgTypes = map[uint16]MsgSubType{
	0x01: TextMsg,
	0x02: ImageMsg,
	0x03: VideoMsg,
	0x04: AudioMsg,
	0x05: LocationMsg,
	0x06: BusinessCardMsg,
	0x07: GroupAnnouncementMsg,
	0x08: EmojiMsg,
	0x09: FileMsg,
	0x10: RedPacketMsg,
}
